package com.clusterswisdom.horn.api.tmall.action.eticket;

import com.alibaba.fastjson.JSONObject;
import com.clusterswisdom.horn.api.ioc.HornAction;
import com.clusterswisdom.horn.api.ioc.HornResponse;
import com.clusterswisdom.horn.api.ioc.api.ApiConfig;
import com.clusterswisdom.horn.api.tmall.TmallInstance;
import com.clusterswisdom.horn.api.tmall.action.AbstractTmallAction;
import com.taobao.api.response.VmarketEticketResendResponse;
import com.taobao.api.response.VmarketEticketSendResponse;

/**
 *（发码）
 * @author Jin_tian
 *
 */
public class SendO2OCodeAction extends AbstractTmallAction implements HornAction{
	
	public static final String METHOD_SEND = "send";
	
	public static final String METHOD_RESEND = "resend";
	
	public static final String METHOD_MODIFIED = "modified";
	
	public static final String SUB_COD = "";
	@Override
	public HornResponse execute(ApiConfig config, JSONObject param) throws Exception {
		String method = (String)param.get("method");
		if (METHOD_SEND.equals(method)) {
			return sendO2OCode(config, param);
			
		} else {
			return resendO2OCode(config, param);
			
		}
	}
	
	private HornResponse sendO2OCode(ApiConfig config, JSONObject param) throws Exception {
		VmarketEticketSendResponse response=(VmarketEticketSendResponse)TmallInstance.getInstance().executeRequest(param, "eticket", "vmarkEteticketSend");
		if(response.isSuccess() && (response.getRetCode()==1)){
			return new HornResponse(true, null);
			
		}else if(response.getSubCode().equals("isv.eticket-send-error:code-alreay-send")){
			return new HornResponse(true, "码已经发过码");
			
		}else{
			return getUnsuccessResponse(response);
			
		}
		
	}
	
	private HornResponse resendO2OCode(ApiConfig config, JSONObject param) throws Exception {
		VmarketEticketResendResponse response=(VmarketEticketResendResponse)TmallInstance.getInstance().executeRequest(param, "eticket", "vmarketEticketResend");
		if(response.isSuccess() && response.getRetCode()==1){
			return new HornResponse(true, null);
			
		}else if(response.getSubCode().equals("isv.eticket-send-error:code-alreay-send")){
			return new HornResponse(true, "码已经发过码");
			
		}else{
			return getUnsuccessResponse(response);
			
		}
		
	}
}
